package pl.un4giv3n.mightvsmagic.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.Clan;
import pl.un4giv3n.mightvsmagic.entity.Item;
import pl.un4giv3n.mightvsmagic.entity.SpecialSkill;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.utils.ClanUtil;
import pl.un4giv3n.mightvsmagic.utils.ImageUtil;
import pl.un4giv3n.mightvsmagic.utils.ItemUtil;
import pl.un4giv3n.mightvsmagic.utils.StatsUtil;
import pl.un4giv3n.mightvsmagic.utils.UserStats;
import pl.un4giv3n.mightvsmagic.utils.UserUtil;

/**
 * @author andrzejewskia
 * show user main page
 */
public class MainPageAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	/**
	 * session user
	 */
	private User user;
	/**
	 * user items to show on the page
	 */
	private List<Item> userItems;
	//item bonuses to show on page
	private long strBonus;
	private long stmBonus;
	private long manaBonus;
	private long attackBonus;
	private long parryBonus;
	private long speedBonus;
	private List<SpecialSkill> userSkills;
	private UserStats stats;
	private long lifeBonus;

	@Override
	public String execute() {
		EntityManager em = getEntityManager();
		user = getSessionUser(em);
		if (user == null) {
			addActionError("Zaloguj się");
			em.close();
			return LOGIN;
		}
		userItems = new ArrayList<Item>(user.getItems());
		userSkills = new ArrayList<SpecialSkill>(user.getSkills());
		Collections.sort(userSkills);
		stats = StatsUtil.getFreshUserStats(user, em);
		em.close();
		for (Item item : userItems) {
			if(item.isInUse()){
				parryBonus+=item.getParryModifier();
				attackBonus+=item.getAttackModifier();
				manaBonus+=item.getManaModifier();
				stmBonus+=item.getStaminaModifier();
				strBonus+=item.getStrenghtModifier();
				speedBonus+=item.getSpeedModifier();
			}
		}

		for (SpecialSkill skill : userSkills) {
				parryBonus+=skill.getParryModifier();
				attackBonus+=skill.getAttackModifier();
				manaBonus+=skill.getManaModifier();
				stmBonus+=skill.getStaminaModifier();
				strBonus+=skill.getStrenghtModifier();
				speedBonus+=skill.getSpeedModifier();
		}
		Clan clan = user.getClan();
		if(clan != null){
			parryBonus+=ClanUtil.getParryBonus(clan);
			attackBonus+=ClanUtil.getAttackBonus(clan);
			manaBonus+=ClanUtil.getManaBonus(clan);
			stmBonus+=ClanUtil.getStaminaBonus(clan);
			strBonus+=ClanUtil.getStrengthBonus(clan);
			speedBonus+=ClanUtil.getSpeedBonus(clan);
			lifeBonus+=ClanUtil.getLifeBonus(clan);
		}
		for (SpecialSkill skill : userSkills) {
			speedBonus=Math.round(speedBonus + speedBonus * skill.getSpeedFactor());
		}

		return SUCCESS;
	}

	// this is the data for the main-page.jsp
	
	public User getUser() {
		return user;
	}

	public List<Item> getUserItems(){
		return userItems;
	}

	public double getLife() {
		return user.getHp();
	}

	public String getBaseDamage() {
		return UserUtil.getBaseMinDamage(user) + "-"
				+ UserUtil.getBaseMaxDamage(user);
	}

	public String getBaseDefense() {
		return UserUtil.getBaseMinDefence(user) + "-"
				+ UserUtil.getBaseMaxDefence(user);
	}

	public String getItemDamage() {
		return ItemUtil.getItemDamage(user);
	}

	public String getItemDefense() {
		return ItemUtil.getItemDefense(user);
	}

	public String getTotalDamage() {
		return UserUtil.getTotalMinDamage(user) + "-"
				+ UserUtil.getTotalMaxDamage(user);
	}

	public String getTotalDefense() {
		return UserUtil.getTotalMinDefense(user) + "-"
				+ UserUtil.getTotalMaxDefense(user);
	}
	
	public String getUserAvatar(){
		return ImageUtil.getUserAvatar(user, getContext().getRealPath("/"));
	}
	
	public String getItemStrenght(){
		if(strBonus<0){
			return ""+strBonus;
		}
		if(strBonus>0){
			return "+"+strBonus;
		}
		return "";
	}
	public String getItemStamina(){
		if(stmBonus<0){
			return ""+stmBonus;
		}
		if(stmBonus>0){
			return "+"+stmBonus;
		}
		return "";
		
	}
	public String getItemMana(){
		if(manaBonus<0){
			return ""+manaBonus;
		}
		if(manaBonus>0){
			return "+"+manaBonus;
		}
		return "";
		
	}
	public String getItemAttack(){
		if(attackBonus<0){
			return ""+attackBonus;
		}
		if(attackBonus>0){
			return "+"+attackBonus;
		}
		return "";
		
	}
	public String getItemParry(){
		if(parryBonus<0){
			return ""+parryBonus;
		}
		if(parryBonus>0){
			return "+"+parryBonus;
		}
		return "";
		
	}
	public String getItemSpeed(){
		if(speedBonus<0){
			return ""+speedBonus;
		}
		if(speedBonus>0){
			return "+"+speedBonus;
		}
		return "";
		
	}

	public List<SpecialSkill> getUserSkills() {
		return userSkills;
	}

	public UserStats getStats() {
		return stats;
	}

	public long getSpeedBonus() {
		return speedBonus;
	}

	public void setSpeedBonus(long speedBonus) {
		this.speedBonus = speedBonus;
	}
	
	public String getUserType(){
		return getText(user.getUserType().name());
	}

	public String getLifeBonus() {
		if(lifeBonus<0){
			return ""+lifeBonus;
		}
		if(lifeBonus>0){
			return "+"+lifeBonus;
		}
		return "";
	}
}
